class Index < ActiveRecord::Base
  belongs_to :category, :foreign_key=>"category_id"
  belongs_to :keyword, :class_name =>"Keyword", :foreign_key=>"keyword_id"
  belongs_to :incumbent, :foreign_key=>"obj_id", :conditions=>"category_id=1"
  belongs_to :report, :foreign_key=>"obj_id", :conditions=>"category_id=2"
  belongs_to :company, :foreign_key=>"obj_id", :conditions=>"category_id=3"
  belongs_to :topic, :foreign_key=>"obj_id", :conditions=>"category_id=4"
  
  def self.index_search_tag(obj)
    tag_arrays=Keyword.split_string(obj.search_tag)
    tag_arrays.each do |word|
      self.create_tag(word,obj)
    end
    tag_arrays=SplitWord.split_word_list(tag_arrays)
    tag_arrays.each do |word|
      self.create_index(word,obj)
    end
  end
  
  def self.clear_search_tag(obj)
    category=Category.find_category(obj)
    Index.find_all(["obj_id=? and category_id=?",obj.id,category.id]).each do |index|
      if index.keyword.class.name="TagWord"
        index.keyword.search_times -= 1
        index.keyword.save
      end
      index.destroy
    end
  end
  
  def self.show_tag_list(obj)
    tag_list=[]
    category=Category.find_category(obj)
    Index.find_all(["obj_id=? and category_id=?",obj.id,category.id]).each do |index|
       tag_list<<index.keyword if index.keyword.class.name=="TagWord"
    end
    return tag_list
  end

  
  def self.create_index(word,obj)
    index=Index.new()
    index.category=Category.find_category(obj)
    index.keyword=SplitWord.find_by_word(word)|| SplitWord.create(:word=>word,
                                                                  :created_at=>Time.now)
    index.obj_id=obj.id
    index.save
  end
  
  def self.create_tag(word,obj)
    index=Index.new()
    index.category=Category.find_category(obj)
    index.keyword= TagWord.find_by_word(word)|| TagWord.create(:word=>word,:search_times=>0,
                                                               :created_at=>Time.now)
    index.keyword.search_times += 1          
    index.obj_id=obj.id
    index.save
    index.keyword.save
  end
  
  def self.create_query_conditions(word_id_list,category_id,additional_conditions="")
    index_ids=[]
    word_id_list.each do |splitword_id|
      if index_ids==[]
        index_ids=index_ids|Index.find(:all,:conditions=>["category_id=? and keyword_id=?",category_id,splitword_id]).collect{|u| u.obj_id}
      else
        index_ids=index_ids&Index.find(:all,:conditions=>["category_id=? and keyword_id=?",category_id,splitword_id]).collect{|u| u.obj_id}
      end
    end
    return Index.create_query(index_ids,additional_conditions)
  rescue
    return "FALSE"
  end


private    
  def self.create_query(index_ids,additional_conditions="")
    unless index_ids==[]
      query_string = "id in("
      index_ids.each {|id| query_string=query_string+id.to_s+","}
      query_string = query_string[0..-2] + ")"
      query_string = query_string +" and " + additional_conditions unless additional_conditions==""
    else
      query_string = "FALSE"
    end
    return query_string
  end
end
